<template>
  <div class="post-detail-page">
    <article class="w-75 mx-auto mb-5 pb-3" v-if="currentPost">
      <img
        :src="currentImageUrl"
        alt="currentPost.title"
        class="rounded-lg img-fluid my-4"
        v-if="currentImageUrl"
      />
      <h2 class="mb-4">{{ currentPost.title }}</h2>
      <div
        class="user-profile-component border-bottom py-3 mb-5 align-items-center row g-0"
      >
        <div class="col">
          <UserProfile
            :user="currentPost.author"
            v-if="typeof currentPost.author === 'object'"
          ></UserProfile>
        </div>
        <span class="text-muted col text-right font-italic"
          >发表于：{{ currentPost.createdAt }}</span
        >
      </div>
      <div v-html="currentHTML"></div>
      <div class="btn-group mt-5" v-if="showeditArea">
        <RouterLink
          type="button"
          class="btn btn-success"
          :to="{ name: 'create', query: { id: currentPost._id } }"
          >编辑</RouterLink
        >
        <button type="button" class="btn btn-danger" @click="showModal">
          删除
        </button>
      </div>
      <ModaIing
        title="删除文章"
        :visible="visible"
        @modal-on-confirm="toConfirm"
        @modal-on-close="toClose"
      >
        <p>确认要删除这篇文章吗？</p>
      </ModaIing>
    </article>
  </div>
</template>
<script lang="ts">
import UserProfile from "../components/UserProfile.vue";
import { defineComponent, computed, onMounted, ref } from "vue";
import MarkdownIt from "markdown-it";
import { PostProps, GolbalDataProps, IamgeProps, ResponseType } from "@/store";
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";
import { UserProps } from "@/store";
import ModaIing from "@/components/ModaIing.vue";
import createMessage from "@/hooks/createMessage";
export default defineComponent({
  components: { UserProfile, ModaIing },
  setup() {
    const store = useStore<GolbalDataProps>();
    const route = useRoute();
    const router=useRouter()
    const currentId = route.params.id;
    const md = new MarkdownIt();
    const currentPost = computed<PostProps>(() =>
      store.getters.getCurrentPost(currentId)
    );
    onMounted(() => {
      store.dispatch("fetchPost", currentId);
    });
    const showeditArea = computed(() => {
      const { isLogin, _id } = store.state.user;
      if (currentPost.value && currentPost.value.author && isLogin) {
        const postAuthor = currentPost.value.author as UserProps;
        return postAuthor._id === _id;
      } else {
        return false;
      }
    });
    // eslint-disable-next-line vue/return-in-computed-property
    const currentHTML = computed(() => {
      const { content } = currentPost.value;
      if (currentPost.value && content) {
        return md.render(content);
      }
    });
    const currentImageUrl = computed(() => {
      if (currentPost.value && currentPost.value.image) {
        const { image } = currentPost.value;
        return (image as IamgeProps).url;
      } else {
        return null;
      }
    });
    //显示弹窗
    const visible = ref(false);
    const showModal = () => {
      visible.value = true;
    };
    const toClose = () => {
      visible.value = false;
    };
    const toConfirm = () => {
      visible.value = false;
      store
        .dispatch("deletePost", currentId)
        .then((rawData: ResponseType<PostProps>) => {
          createMessage("删除成功 2秒后跳转到专栏页", "success")
          setTimeout(()=>{
            router.push({name:'column',params:{id:rawData.data.column}})
          },2000)
        });
    };
    return {
      currentPost,
      currentHTML,
      currentImageUrl,
      showeditArea,
      showModal,
      visible,
      toClose,
      toConfirm,
    };
  },
});
</script>

<style scoped></style>
